package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.ReportAttendancePageBean;
import com.attendance.www.hr.beans.ReportAttendanceParam;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IAttendanceReportService;
import com.attendance.www.hr.service.IReportExportService;
import com.attendance.www.hr.service.IReportResultService;
import com.attendance.www.hr.service.IReportService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 报表
 * @author 万波   张鑫
 *
 */
@Controller
@RequestMapping("/report")
public class ReportController {

	@Resource
	private IReportService reportService;
	@Resource
	private IReportResultService reportResultService;
	@Resource
	private IReportExportService reportExportService;
	@Resource
	private IAttendanceReportService attendanceReportService;
	
	/**
	 * 导出报表
	 * @param request
	 * @param paramBean
	 * @return
	 */
	@RequestMapping(value = "/getExportReport.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object getExportReport(HttpServletRequest request,@RequestBody ReportAttendanceParam paramBean) {
		if (paramBean.getBeginDate()==null
			||paramBean.getEndDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		
		//根据权限查看报表
		if (employeeId != null) {
			paramBean=reportService.getReportByAuthority(paramBean, employeeId);
		}
		
		return ResultMap.success(reportExportService.getExportReport(paramBean));
	}
	
	/**
	 * 查询考勤报表
	 * @param request
	 * @param restVacationBean
	 * @return
	 */
	@RequestMapping(value = "/getReportAttendanceList.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object getReportAttendanceList(HttpServletRequest request,@RequestBody ReportAttendanceParam paramBean) {
		if (paramBean.getPageCount()==null
			||paramBean.getPageNo()==null
			||paramBean.getBeginDate()==null
			||paramBean.getEndDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		
		//根据权限查看报表
		if (employeeId != null) {
			paramBean=reportService.getReportByAuthority(paramBean, employeeId);
		}
		Integer startNum = 0;
		Integer pageNo = paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		paramBean.setStartNum(startNum);
		ReportAttendancePageBean rap=reportResultService.getReportResultInfoList(paramBean);
		return ResultMap.success(rap);
	}
	
	/**
	 * 每日生成考勤报表数据
	 * @param request
	 * @param paramBean
	 * @return
	 */
	@RequestMapping(value = "/saveAttendanceReport.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object saveAttendanceReport(HttpServletRequest request,@RequestBody ReportAttendanceParam paramBean) {
		if (paramBean.getBeginDate()==null
			||paramBean.getEndDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
//		attendanceReportService.saveAttendanceReport(paramBean.getBeginDate(),paramBean.getEndDate());
		attendanceReportService.saveAttendanceReportMonth(paramBean.getBeginDate());
		return ResultMap.success();
	}
	
}
